import socket
import select
from Protocol import *

class MySocket:
    def __init__(self):
        self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self._sendBuf = ''

    def connect(self, host, port):
        self._socket.connect((host, port))
        self._socket.setblocking(0)

    def send(self, msg):
    	print list(msg)
        self._sendBuf = self._sendBuf + str(msg)

    def run(self, timeout=10):
        ready_to_read, ready_to_write, in_error = \
                       select.select([self._socket], [self._socket], [self._socket], timeout)
        if len(ready_to_write)>0 and len(self._sendBuf):
            #print self._sendBuf            
            self._socket.send(self._sendBuf)
            self._sendBuf = ''
            pass
        if len(ready_to_read)>0:
            print "read"

cqSocket = MySocket()
cqSocket.connect("116.211.22.62", 7000)
cliMsg = TClientMessage()
cliMsg.Sign = 0xFF44FF44
cliMsg.Cmd =24
print list(cliMsg.byteBuff)
print '[[[[[[[[[[[['
cliMsg.DataLength = 0
cliMsg.DataIndex = 0
print list(cliMsg.byteBuff)
print ']]]]]]'
cqSocket.send(cliMsg.byteBuff)
while True:
    cqSocket.run()

        
